VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Detail"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'/******************************************************************/
'/*                                                                */
'/*                      TurboCAD for Windows                      */
'/*                   Copyright (c) 1993 - 2001                    */
'/*             International Microcomputer Software, Inc.         */
'/*                            (IMSI)                              */
'/*                      All rights reserved.                      */
'/*                                                                */
'/******************************************************************/

Option Explicit

'DBAPI constants
Const gkGraphic = 11
Const gkArc = 2
Const gkText = 6
Const gfCosmetic = 128&
                                                                                                                                                                                             
'Useful math constants
Const Pi# = 3.14159265
Const Eps = 0.0001
'Real variant types!
Const typeEmpty = 0
Const typeInteger = 2
Const typeLong = 3
Const typeSingle = 4
Const typeDouble = 5
Const typeCurrency = 6
Const typeDate = 7
Const typeString = 8
Const typeObject = 9
Const typeBoolean = 11
Const typeVariant = 12
Const typeIntegerEnum = typeInteger + 100
Const typeLongEnum = typeLong + 100
Const typeStringEnum = typeString + 100

'Stock property pages
Const ppStockPen = 1
Const ppStockBrush = 2
Const ppStockText = 4
Const ppStockInsert = 8
Const ppStockViewport = 16
Const ppStockAuto = 32

'Property Ids
Const idDiameter = 1
Const idLength = 2
Const idBType = 3
Const idLengthThread = 4

Const idDiameterOld = 5
Const idLengthOld = 6
Const idBTypeOld = 7
Const idLengthThreadOld = 8

Const idSolid = 9
Const idSolidOld = 10


'Property enums
Const NUM_TYPES = 0

'Number of properties, pages, wizards
Const NUM_PROPERTIES = 10
Const NUM_PAGES = 1
Const NUM_WIZARDS = 0

Private PColor As Long
Private BColor As Long


Private Sub Class_Initialize()
    'Initialize class variables
End Sub

'Returns the user-visible description of this RegenMethod
Public Property Get Description() As String
    Description = "SDK Bolt_Screw3D"
End Property

'Returns the persistent class id for this RegenMethod's property section
Public Property Get ClassID() As String
    ClassID = "{FDB6F1C3-9631-11d1-A40A-0000B465872B}"
End Property

'Retrieve types and names
Public Function GetPropertyInfo(Names As Variant, Types As Variant, _
    IDs As Variant, Defaults As Variant) As Long
    ReDim Names(NUM_PROPERTIES), Types(NUM_PROPERTIES), _
        IDs(NUM_PROPERTIES), Defaults(NUM_PROPERTIES)
        
    Names(0) = "Diameter"
    Types(0) = typeDouble
    IDs(0) = idDiameter
    Defaults(0) = 1#

    Names(1) = "Length"
    Types(1) = typeDouble
    IDs(1) = idLength
    Defaults(1) = 3#
    
    Names(2) = "BType"
    Types(2) = typeString
    IDs(2) = idBType
    Defaults(2) = "HexBolt"
   
    Names(3) = "LengthThread"
    Types(3) = typeDouble
    IDs(3) = idLengthThread
    Defaults(3) = 1.5
   
    Names(4) = "DiameterOld"
    Types(4) = typeDouble
    IDs(4) = idDiameterOld
    Defaults(4) = 1.5

    Names(5) = "LengthOld"
    Types(5) = typeDouble
    IDs(5) = idLengthOld
    Defaults(5) = 3#
    
    Names(6) = "BTypeOld"
    Types(6) = typeString
    IDs(6) = idBTypeOld
    Defaults(6) = "HexBolt"
   
    Names(7) = "LengthThreadOld"
    Types(7) = typeDouble
    IDs(7) = idLengthThreadOld
    Defaults(7) = 1.5
    
    Names(8) = "BoltSolid"
    Types(8) = typeInteger
    IDs(8) = idSolid
    Defaults(8) = 1
    
    Names(9) = "BoltSolidOld"
    Types(9) = typeInteger
    IDs(9) = idSolidOld
    Defaults(9) = 1
   
   GetPropertyInfo = NUM_PROPERTIES
End Function

'Get the number of property pages supporting this RegenMethod
Public Function GetPageInfo(ByVal AGraphic As Object, StockPages As Long, _
    Names As Variant) As Long
    ReDim Names(NUM_PAGES)

    'Need the form
'    Load frmBolt3D
'    Names(0) = frmBolt3D.Caption
'    Unload frmBolt3D
' load from resources
    Names(0) = LoadResString(101)
    StockPages = ppStockBrush + ppStockPen + ppStockAuto
    GetPageInfo = NUM_PAGES
End Function

Public Function GetWizardInfo(Names As Variant) As Long
    ReDim Names(NUM_WIZARDS)
    GetWizardInfo = NUM_WIZARDS
End Function

'Enumerate the names and values of a specified property
Public Function GetEnumNames(ByVal PropID As Long, Names As Variant, Values As Variant) As Long
        GetEnumNames = 0
End Function

Public Function PageControls(ByVal ThisRegenMethod As Object, ByVal Graphic As Object, ByVal PageNumber As Long, ByVal SaveProperties As Boolean) As Boolean
        'Set up error function
        On Error GoTo Failed
        Dim Diameter#, Length#, LengthThread#
        Dim SSolid%
        If SaveProperties Then
            'OK button on property page was clicked
            'Form is still loaded
            With frmBolt3D
                'Need On Error statement for the case where you have
                'RRect Turbo Shape and ahother "shape" selected
                On Error Resume Next
                'When the property page is closed, transfer the numeric
                'Diameter value from the TextBox to the Graphic
                'Get the value as a double-precision number
                Diameter# = CDbl(.txtdd.Text)
                Length# = CDbl(.txtLen.Text)
                LengthThread# = CDbl(.txtThread.Text)
                TypeB = .List1.Text
                 If .Surf_Solid.Value = True Then ' if surface
                    SSolid = 0
                 ElseIf .Surf_Solid1.Value = True Then
                    SSolid = 1
                 End If
                
                'Make sure it's between 0 and 100
                If Diameter# < 0# Then Diameter# = 1#
                If LengthThread > Length Then LengthThread = Length
                'Set the roundness property value in the Graphic
                Graphic.Properties("Diameter") = Diameter#
                Graphic.Properties("Length") = Length#
                Graphic.Properties("LengthThread") = LengthThread#
                Graphic.Properties("BType") = TypeB
                Graphic.Properties("BoltSolid") = SSolid
            End With
        Else
            'Property page is about to be opened
            'Make sure the form is loaded
                Diameter# = Graphic.Properties("Diameter")
                Length# = Graphic.Properties("Length")
                LengthThread = Graphic.Properties("LengthThread")
                TypeB = Graphic.Properties("BType")
                SSolid = Graphic.Properties("BoltSolid")
            Load frmBolt3D
            With frmBolt3D
            
                'If more than one RRect is selected and they do not
                'have the same properties, don't set up this field
                On Error GoTo NoRType

                'When the property page is opening, transfer the numeric
                'roundness value from the Graphic to the TextBox
                'Get the roundness property value from the Graphic
                'Set the TextBox control's text
                .txtdd.Text = Diameter#
                .txtLen = Length#
                .txtThread.Text = LengthThread#
                 If SSolid = 1 Then
                    .Surf_Solid1.Value = True
                    .Surf_Solid.Value = False
                 ElseIf SSolid = 0 Then
                    .Surf_Solid1.Value = False
                    .Surf_Solid.Value = True
                 End If

NoRType:
            End With
        End If

        PageControls = True
        Exit Function

Failed:
        'For debugging purposes, report that an error occurred
        If Err.Number <> 0 Then
            MsgBox "Error in PageControls: " & Err.Description
        End If

        'Return false if an error occurred
        PageControls = False
End Function

Public Function PageDone(ByVal ThisRegenMethod As Object, Optional PageNumber As Variant)
        'Done with form
        Unload frmBolt3D
End Function

Public Function PropertyPages(ByVal ThisRegenMethod As Object, Optional PageNumber As Variant) As Boolean
    With frmBolt3D
        .Show vbModal
        PropertyPages = Not .DialogCanceled
    End With
End Function

Public Function Wizard(ByVal ThisRegenMethod As Object, Optional WizardNumber As Variant) As Boolean
    Wizard = False
End Function

'Called when vertex has been moved, or other geometry change
Public Function OnGeometryChanged(ByVal Graphic As Object, ByVal GeomID As Long, paramOld As Variant, paramNew As Variant)
    'Do nothing
End Function

'Called when vertex is moved, or other geometry change
Public Function OnGeometryChanging(ByVal Graphic As Object, ByVal GeomID As Long, paramOld As Variant, paramNew As Variant) As Boolean
    'OK to continue with change
    OnGeometryChanging = True
End Function

Public Function OnNewGraphic(ByVal grfThis1 As Object, ByVal boolCopy As Boolean) As Boolean
    If boolCopy Then
        'Vertices are already added for us...
        OnNewGraphic = True
        Exit Function
    End If
Dim grfThis As Graphic
    Set grfThis = grfThis1
    If grfThis.Application.ActiveDrawing.Properties("TileMode") <> imsiModelSpace Then
        GoTo Failed
    End If
    On Error GoTo Failed
    'New Graphic being created
    'X, Y, Z, PenDown, Selectable, Snappable, Editable, Linkable
    'First Vertex is "first point of axis"
    grfThis.Vertices.UseWorldCS = True
    grfThis.Vertices.Add 2#, 5#, 0#, False, , , False
    
    'Second Vertex is "Second point of axis"
    grfThis.Vertices.Add 6#, 5#, 0#, False, , , False
    
    OnNewGraphic = True
    Exit Function

Failed:
    'Return false on failure
    OnNewGraphic = False
End Function

'Function called whenever a copy of a graphic is being made
Public Function OnCopyGraphic(ByVal grfCopy As Object, ByVal grfSource As Object) As Boolean
    'Return false on failure
    OnCopyGraphic = True
End Function

'Notification function called after graphic property is saved
Public Function OnPropertyChanged(ByVal Graphic As Object, ByVal PropID As Long, _
        ValueOld As Variant, ValueNew As Variant)
    'Do nothing
End Function

'Notification function called when graphic property is saved
Public Function OnPropertyChanging(ByVal Graphic As Object, ByVal PropID As Long, _
        ValueOld As Variant, ValueNew As Variant) As Boolean
    'OK to proceed
    OnPropertyChanging = True
End Function

'Notification function called when graphic property is retrieved
Public Function OnPropertyGet(ByVal Graphic As Object, ByVal PropID As Long)
    'Do nothing
End Function

'Called when we need to update our object
Public Function Regen(ByVal grfThis1 As Object)
        'Setup error handler
        On Error GoTo Failed
Dim grfThis As Graphic
        Set grfThis = grfThis1
        'Set up lock (prevent recursion)
Dim LockCount&
        LockCount& = grfThis.RegenLock
        'Setup error handler (make sure lock is removed)
        On Error GoTo FailedLock
        If LockCount& = 0 Then
            'Delete any previous cosmetic children
Dim dd#
            dd = grfThis.Properties("Diameter")
Dim TypeBb
            TypeBb = grfThis.Properties("BType")
                
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dim Salp#, Calp#, L#
Dim X00#, Y00#, Z00#, X01#, Y01#, Z01#
Dim LenProp#
            LenProp = grfThis.Properties("Length")
Dim lThread#
            lThread = grfThis.Properties("LengthThread")
            If lThread > LenProp Then
                lThread = LenProp
                grfThis.Properties("LengthThread") = lThread
            End If
            grfThis.Vertices.UseWorldCS = True
            With grfThis.Vertices
                X00 = .Item(0).X
                Y00 = .Item(0).Y
                Z00 = .Item(0).Z
                X01 = .Item(1).X
                Y01 = .Item(1).Y
                Z01 = .Item(1).Z
                L = Sqr((X01 - X00) * (X01 - X00) + (Y01 - Y00) * (Y01 - Y00) + (Z01 - Z00) * (Z01 - Z00))
                Salp = (Y01 - Y00) / L
                Calp = (X01 - X00) / L
            End With
Dim ddOld#
            ddOld = grfThis.Properties("DiameterOld")
Dim LOld#
            LOld = grfThis.Properties("LengthOld")
Dim LThreadOld#
            LThreadOld = grfThis.Properties("LengthThreadOld")
Dim TypeBOld$
            TypeBOld = grfThis.Properties("BTypeOld")
Dim SSolid%, SolidOld%
            SSolid = grfThis.Properties("BoltSolid")
            SolidOld = grfThis.Properties("BoltSolidOld")
        
Dim CountCosm As Long, iCosm As Long
            CountCosm = grfThis.Graphics.Count
            If CountCosm > 1 Then
                For iCosm = 1 To CountCosm - 1
                    grfThis.Graphics(iCosm).Properties("PenColor") = grfThis.Properties("PenColor")
                Next iCosm
            End If
            If grfThis.Properties("Selected") = False Or SSolid <> SolidOld Or Abs(L - LenProp) > Eps Or Abs(L - LOld) > Eps Or Abs(dd - ddOld) > Eps Or Abs(lThread - LThreadOld) > Eps Or TypeBb <> TypeBOld Then
                L = LenProp
                grfThis.Graphics.Clear gfCosmetic
' Block for Scaling
Dim AddScale#
                AddScale = 1
                dd = dd * AddScale
                L = L * AddScale
                lThread = lThread * AddScale
                
                If TypeBb = "HexBolt" Then Call HexBolt(grfThis, dd, L, Salp, Calp, lThread, SSolid)
                If TypeBb = "SlottedBolt" Then Call SlottedBolt(grfThis, dd, L, Salp, Calp, lThread, SSolid)
                If TypeBb = "HexFlScrew" Then Call HexFlScrew(grfThis, dd, L, Salp, Calp, lThread, SSolid)
' Block for Unscaling
                dd = dd / AddScale
                L = L / AddScale
                lThread = lThread / AddScale
                
                grfThis.Properties("DiameterOld") = dd
                grfThis.Properties("LengthOld") = L
                grfThis.Properties("LengthThreadOld") = lThread
                grfThis.Properties("DiameterOld") = dd
                grfThis.Properties("BTypeOld") = TypeBb
                grfThis.Properties("BoltSolidOld") = SSolid
            End If
            
        End If

        
        grfThis.RegenUnlock
        Exit Function

FailedLock:
        'Remove lock
        grfThis.RegenUnlock

Failed:
End Function

Public Function Draw(ByVal grfThis As Object, ByVal view As Object, Optional mat As Variant) As Boolean
    'Return True if we did the redraw (no further processing necessary, no children will be drawn).
    'Since this is just a test, we return False to let TurboCAD do the drawing operation.
    Draw = False
End Function
Private Function Angle(sinb As Double, cosb As Double) As Double

        If Abs(cosb) < 0.0001 Then
            If sinb > 0 Then
                Angle = Pi / 2
            Else
                Angle = 3 * Pi / 2
            End If
        Else
            If sinb >= 0 And cosb > 0 Then Angle = Atn(sinb / cosb)
            If sinb >= 0 And cosb < 0 Then Angle = Pi + Atn(sinb / cosb)
            If sinb < 0 And cosb < 0 Then Angle = Pi + Atn(sinb / cosb)
            If sinb < 0 And cosb > 0 Then Angle = 2 * Pi + Atn(sinb / cosb)
        End If
End Function

' HexBolt

Private Sub HexBolt(Gr As Graphic, dd As Double, L As Double, Salp As Double, Calp As Double, lThread As Double, SSolid As Integer)
On Error GoTo Failed
Dim D#, H#, T#, J#, del#, E#, F#
        D = 1.75 * dd
        H = 0.65 * dd
        E = 0.875 * H
        F = D / 4#
        del = 0.1 * dd

Dim Grs As Graphics
    Set Grs = Gr.Application.ActiveDrawing.Graphics
Dim X00#, Y00#, Z00#, X01#, Y01#, Z01#
Dim AddScale#, L1#
        AddScale = 1
        Gr.Vertices.UseWorldCS = True
        With Gr.Vertices
            X00 = .Item(0).X
            Y00 = .Item(0).Y
            Z00 = .Item(0).Z
            X01 = .Item(1).X
            Y01 = .Item(1).Y
            Z01 = .Item(1).Z
            L1 = Sqr((X01 - X00) * (X01 - X00) + (Y01 - Y00) * (Y01 - Y00) + (Z01 - Z00) * (Z01 - Z00))
            If Abs(L + H - L1) > 0.001 Then
                .Item(0).X = X00 - (X01 - X00) * H / AddScale / L1
                .Item(0).Y = Y00 - (Y01 - Y00) * H / AddScale / L1
                .Item(0).Z = Z00 - (Z01 - Z00) * H / AddScale / L1
                .Item(1).X = X00 + (X01 - X00) * L / AddScale / L1
                .Item(1).Y = Y00 + (Y01 - Y00) * L / AddScale / L1
                .Item(1).Z = Z00 + (Z01 - Z00) * L / AddScale / L1
                X01 = X00 + (X01 - X00) * L / L1
                Y01 = Y00 + (Y01 - Y00) * L / L1
                Z01 = Z00 + (Z01 - Z00) * L / L1
            End If
        End With

Dim X00New#, Y00New#, Z00New#, X01New#, Y01New#, Z01New#
        X00New = X00
        Y00New = Y00
        Z00New = Z00
        X01New = X01
        Y01New = Y01
        Z01New = Z01
        X00 = 0
        Y00 = 0
        Z00 = 0
        X01 = L
        Y01 = 0
        Z01 = 0
        
Dim X0(100)
Dim Y0(100)
'################################################################
'Cylinder
       X0(1) = -0.5 * H
       Y0(1) = 0#
       
       X0(2) = -0.5 * H
       Y0(2) = dd / 2
       
       X0(3) = L - del
       Y0(3) = dd / 2#
       
       X0(4) = 1 * L
       Y0(4) = dd / 2 - del
       
       X0(5) = 1 * L
       Y0(5) = 0#
       
       X0(6) = X0(1)
       Y0(6) = Y0(1)

Dim i%
Dim Gr1 As Graphic
Dim GrTemp As Graphic
'Add child Graphics revolve Contour
            Set GrTemp = Grs.Add(gkGraphic)
            With GrTemp.Vertices
                For i = 1 To 6
                    .Add X0(i), Y0(i), 0
                Next i
            End With
            GrTemp.Closed = True
            Set Gr1 = Grs.Add(, "TCW40SPIN")
            Gr1.Properties("Solid") = SSolid
            Gr1.Properties("$ROTATIONANGLE") = 2 * Pi
            Gr1.Properties("$ROTATIONCOPY") = 30
            Set GrTemp = Grs.Remove(GrTemp.Index)
            Gr1.Graphics.AddGraphic GrTemp
            Gr1.Vertices.Add X0(1), Y0(1), 0, False, False, False, False, False
            Gr1.Vertices.Add X0(5), Y0(5), 0, False, False, False, False, False
'################################################################
' Thread
    'Base contour
Dim hThread# ' Step of the thread
    hThread = del / Sin(Pi / 3)
Dim nCoil As Long, k As Long
    nCoil = 10
    nCoil = CLng(lThread / hThread)
    
Dim lThreadNew#
    lThreadNew = nCoil * hThread
Dim k1 As Long
    X0(1) = L#
    Y0(1) = dd#
    
    X0(2) = L#
    Y0(2) = dd / 2
    k1 = 2
    For i = 1 To nCoil
        k1 = k1 + 1
        X0(k1) = X0(k1 - 1) - hThread / 2
        Y0(k1) = Y0(k1 - 1) - del
        k1 = k1 + 1
        X0(k1) = X0(k1 - 1) - hThread / 2
        Y0(k1) = Y0(k1 - 1) + del
    Next i
    
    k1 = k1 + 1
    X0(k1) = L - lThreadNew
    Y0(k1) = dd
    
    k1 = k1 + 1
    X0(k1) = L
    Y0(k1) = dd
Set GrTemp = Grs.Add(gkGraphic)
With GrTemp.Vertices
    For i = 1 To k1
        .Add X0(i), Y0(i), 0
    Next i
End With
GrTemp.Closed = True
Dim Gr2 As Graphic
Set Gr2 = Grs.Add(, "TCW40SPIN")
Gr2.Properties("Solid") = SSolid
Gr2.Properties("$ROTATIONANGLE") = 2 * Pi
Gr2.Properties("$ROTATIONCOPY") = 30
Set GrTemp = Grs.Remove(GrTemp.Index)
Gr2.Graphics.AddGraphic GrTemp
Gr2.Vertices.Add X01, Y01, 0, False, False, False, False, False
Gr2.Vertices.Add X00, Y00, 0, False, False, False, False, False
'###############################################################
' Head of the bolt
    For i = 7 To 13
        X0(i) = D / 2 * Cos(Pi / 3 * (i - 7))
        Y0(i) = D / 2 * Sin(Pi / 3 * (i - 7))
    Next i
Dim Gr3 As Graphic
Set Gr3 = Grs.Add(gkGraphic)
With Gr3.Vertices
    For i = 7 To 13
        .Add X0(i), Y0(i), 0, True, True, False, False, False, False
    Next i
End With
Gr3.Closed = True
Gr3.Properties("Thickness") = H
Gr3.Properties("Solid") = SSolid
Dim xTo#, yTo#, zTo#, xFrom#, yFrom#, zFrom#, xRef#, yRef#, zRef#
    xTo = X00 + (X01 - X00) / L
    yTo = Y00 + (Y01 - Y00) / L
    zTo = 0
    xFrom = X00
    yFrom = Y00
    zFrom = 1#
    xRef = X00
    yRef = Y00
    zRef = 0#
    Gr3.RotateAbsolute xTo, yTo, zTo, xFrom, yFrom, zFrom, xRef, yRef, zRef
'##################################################################
 'faska on head
    T = D / 2 * Sin(Pi / 3)
       X0(1) = -1.1 * H
       Y0(1) = T * 0.7 - 0.1 * H / Tan(Pi / 6)
       
       X0(2) = -1.1 * H
       Y0(2) = D
       
       X0(3) = -H + 0.4 * H
       Y0(3) = D
       
       X0(4) = -H + 0.4 * H
       Y0(4) = T * 0.7 + 0.5 * H / Tan(Pi / 6)
       
       X0(5) = X0(1)
       Y0(5) = Y0(1)
       
'-----------------------------------------------------------
       
            Set GrTemp = Grs.Add(gkGraphic)
            With GrTemp.Vertices
                For i = 1 To 5
                    .Add X0(i), Y0(i), 0
                Next i
            End With
            GrTemp.Closed = True
Dim Gr4 As Graphic
            Set Gr4 = Grs.Add(, "TCW40SPIN")
            Gr4.Properties("Solid") = SSolid
            Gr4.Properties("$ROTATIONANGLE") = 2 * Pi
            Gr4.Properties("$ROTATIONCOPY") = 30
            Set GrTemp = Grs.Remove(GrTemp.Index)
            Gr4.Graphics.AddGraphic GrTemp
            Gr4.Vertices.Add X00, Y00, 0, False, False, False, False, False
            Gr4.Vertices.Add X01, Y01, 0, False, False, False, False, False
'#################################################################
' Boolean operations
Dim Bool3D As Boolean3D
    Set Bool3D = New Boolean3D
Dim Res As Long
Dim GrIndex As Long
    
Dim GrRes1 As Graphic
    Gr1.Update
    Gr2.Update
    Set GrRes1 = Bool3D.Subtract(Gr1, Gr2)

    If (Not GrRes1 Is Nothing) Then
        Gr1.Deleted = True
        Gr2.Deleted = True
        Grs.AddGraphic GrRes1
    End If
 Dim GrRes2 As Graphic

    GrRes1.Update
    Gr3.Update
    Set GrRes2 = Bool3D.Add(GrRes1, Gr3)
    If (Not GrRes2 Is Nothing) Then
        GrRes1.Deleted = True
        Gr3.Deleted = True
        Grs.AddGraphic GrRes2
    End If
   
 Dim GrRes3 As Graphic
    GrRes2.Update
    Gr4.Update
    Set GrRes3 = Bool3D.Subtract(GrRes2, Gr4)
    If (Not GrRes3 Is Nothing) Then
        GrRes2.Deleted = True
        Gr4.Deleted = True
        Grs.AddGraphic GrRes3
    End If
    
    GrIndex = GrRes3.Index
    Grs.Remove GrIndex
    Gr.Graphics.AddGraphic GrRes3
    GrRes3.Cosmetic = True
    GrRes3.Properties("PenColor") = Gr.Properties("PenColor")
'?????????????????????????????????????????????????????????????
Dim GrLine As Graphic
    Set GrLine = Grs.Add(11)
    GrLine.Vertices.UseWorldCS = True
    With GrLine.Vertices
        .Add -H, 0, 0
        .Add L, 0, 0
    End With
    GrLine.Properties("PenColor") = RGB(255, 0, 0)
    GrIndex = GrLine.Index
    Grs.Remove GrIndex
    Gr.Graphics.AddGraphic GrLine
    GrLine.Cosmetic = True
Dim xx0#, yy0#, zz0#, xx1#, yy1#, zz1#

    GrLine.Vertices.UseWorldCS = False
    With GrLine.Vertices
        xx0 = .Item(0).X
        yy0 = .Item(0).Y
        zz0 = .Item(0).Z
        xx1 = .Item(1).X
        yy1 = .Item(1).Y
        zz1 = .Item(1).Z
    End With
    Gr.Vertices.UseWorldCS = False
    With Gr.Vertices
        X00New = .Item(0).X
        Y00New = .Item(0).Y
        Z00New = .Item(0).Z
   End With
    GrRes3.MoveRelative X00New - xx0, Y00New - yy0, Z00New - zz0
    GrLine.Deleted = True
'?????????????????????????????????????????????????????????????

    Set Gr1 = Nothing
    Set Gr2 = Nothing
    Set Gr3 = Nothing
    Set Gr4 = Nothing
    Set GrRes1 = Nothing
    Set GrRes2 = Nothing
    Set GrRes3 = Nothing
    Set GrTemp = Nothing
    Set Grs = Nothing
    Set Bool3D = Nothing
    
Exit Sub
Failed:

        If Err.Number <> 0 Then
            MsgBox "Regen error: " & Err.Description
        End If
End Sub


' Slotted Bolt

Private Sub SlottedBolt(Gr As Graphic, dd As Double, L As Double, Salp As Double, Calp As Double, lThread As Double, SSolid As Integer)
On Error GoTo Failed
Dim D#, H#, T#, J#, del#, E#, F#
        D = 1.875 * dd
        H = 0.494 * dd
        T = 0.24 * dd
        J = 0.19 * dd
        del = 0.1 * dd

Dim Grs As Graphics
    Set Grs = Gr.Application.ActiveDrawing.Graphics
Dim X00#, Y00#, Z00#, X01#, Y01#, Z01#
Dim AddScale#, L1#
        AddScale = 1
        Gr.Vertices.UseWorldCS = True
        With Gr.Vertices
            X00 = .Item(0).X
            Y00 = .Item(0).Y
            Z00 = .Item(0).Z
            X01 = .Item(1).X
            Y01 = .Item(1).Y
            Z01 = .Item(1).Z
            L1 = Sqr((X01 - X00) * (X01 - X00) + (Y01 - Y00) * (Y01 - Y00) + (Z01 - Z00) * (Z01 - Z00))
            .Item(1).X = X00 + (X01 - X00) * L / AddScale / L1
            .Item(1).Y = Y00 + (Y01 - Y00) * L / AddScale / L1
            .Item(1).Z = Z00 + (Z01 - Z00) * L / AddScale / L1
            X01 = X00 + (X01 - X00) * L / L1
            Y01 = Y00 + (Y01 - Y00) * L / L1
            Z01 = Z00 + (Z01 - Z00) * L / L1
        End With
Dim X00New#, Y00New#, Z00New#, X01New#, Y01New#, Z01New#
        X00New = X00
        Y00New = Y00
        Z00New = Z00
        X01New = X01
        Y01New = Y01
        Z01New = Z01
        X00 = 0
        Y00 = 0
        Z00 = 0
        X01 = L
        Y01 = 0
        Z01 = 0
        

Dim X0(100)
Dim Y0(100)
       
       X0(1) = 0.9 * H
       Y0(1) = 0#
       
       X0(2) = 0.9 * H
       Y0(2) = dd / 2
       
       X0(3) = L - del
       Y0(3) = dd / 2#
       
       X0(4) = 1 * L
       Y0(4) = dd / 2 - del
       
       X0(5) = 1 * L
       Y0(5) = 0#
       
       X0(6) = X0(1)
       Y0(6) = Y0(1)
'-----------------------------------------------------------
       
Dim i%
       
Dim Gr1 As Graphic
Dim GrTemp As Graphic
'Add child Graphics revolve Contour
            Set GrTemp = Grs.Add(gkGraphic)
            With GrTemp.Vertices
                For i = 1 To 6
                    .Add X0(i), Y0(i), 0
                Next i
            End With
            GrTemp.Closed = True
            Set Gr1 = Grs.Add(, "TCW40SPIN")
            Gr1.Properties("Solid") = SSolid
            Gr1.Properties("$ROTATIONANGLE") = 2 * Pi
            Gr1.Properties("$ROTATIONCOPY") = 30
            Set GrTemp = Grs.Remove(GrTemp.Index)
            Gr1.Graphics.AddGraphic GrTemp
            Gr1.Vertices.Add X0(1), Y0(1), 0, False, False, False, False, False
            Gr1.Vertices.Add X0(5), Y0(5), 0, False, False, False, False, False
            
' Thread
    'Base contour
Dim hThread# ' Step of the thread
    hThread = del / Sin(Pi / 3)
Dim nCoil As Long, k As Long
    nCoil = 10
    nCoil = CLng(lThread / hThread)
    
Dim lThreadNew#
    lThreadNew = nCoil * hThread
Dim k1 As Long
    
    X0(1) = L#
    Y0(1) = dd#
    
    X0(2) = L#
    Y0(2) = dd / 2
    
    k1 = 2
    
    For i = 1 To nCoil
        k1 = k1 + 1
        X0(k1) = X0(k1 - 1) - hThread / 2
        Y0(k1) = Y0(k1 - 1) - del
        k1 = k1 + 1
        X0(k1) = X0(k1 - 1) - hThread / 2
        Y0(k1) = Y0(k1 - 1) + del
    Next i
    k1 = k1 + 1
    X0(k1) = L - lThreadNew
    Y0(k1) = dd
    
    k1 = k1 + 1
    X0(k1) = L
    Y0(k1) = dd

    Set GrTemp = Grs.Add(gkGraphic)
    With GrTemp.Vertices
        For i = 1 To k1
            .Add X0(i), Y0(i), 0
        Next i
    End With
    GrTemp.Closed = True

Dim Gr2 As Graphic
    Set Gr2 = Grs.Add(, "TCW40SPIN")
    Gr2.Properties("Solid") = SSolid
    Gr2.Properties("$ROTATIONANGLE") = 2 * Pi
    Gr2.Properties("$ROTATIONCOPY") = 30
    Set GrTemp = Grs.Remove(GrTemp.Index)
    Gr2.Graphics.AddGraphic GrTemp
    Gr2.Vertices.Add X01, Y01, 0, False, False, False, False, False
    Gr2.Vertices.Add X00, Y00, 0, False, False, False, False, False

'############################################################
'############################################################
' Head of the bolt

       X0(1) = 0#
       Y0(1) = 0#
       
       X0(2) = 0#
       Y0(2) = D / 2#
       
       X0(3) = H
       Y0(3) = dd / 2#
       
       X0(4) = H
       Y0(4) = 0#
       
       X0(5) = X0(1)
       Y0(5) = Y0(1)
       
        Set GrTemp = Grs.Add(gkGraphic)
            
        With GrTemp.Vertices
            For i = 1 To 5
                .Add X0(i), Y0(i), 0
            Next i
        End With
        GrTemp.Closed = True
Dim Gr3 As Graphic
        Set Gr3 = Grs.Add(, "TCW40SPIN")
        Gr3.Properties("Solid") = SSolid
        Gr3.Properties("$ROTATIONANGLE") = 2 * Pi
        Gr3.Properties("$ROTATIONCOPY") = 30
        Set GrTemp = Grs.Remove(GrTemp.Index)
        Gr3.Graphics.AddGraphic GrTemp
        Gr3.Vertices.Add X0(1), Y0(1), 0, False, False, False, False, False
        Gr3.Vertices.Add X0(4), Y0(4), 0, False, False, False, False, False
       
'################################################################
'################################################################

       X0(1) = -0.1 * H
       Y0(1) = 1.1 * D / 2
       
       X0(2) = T
       Y0(2) = 1.1 * D / 2
       
       X0(3) = T
       Y0(3) = -1.1 * D / 2
       
       X0(4) = -0.1 * H
       Y0(4) = -1.1 * D / 2
       
       X0(5) = X0(1)
       Y0(5) = Y0(1)
       
'-----------------------------------------------------------
Dim Gr4 As Graphic
            Set Gr4 = Grs.Add(gkGraphic)
            With Gr4.Vertices
                For i = 1 To 5
                    .Add X0(i), Y0(i), 0
                Next i
            End With
            Gr4.Closed = True
            Gr4.Properties("Thickness") = J
            Gr4.Properties("Solid") = SSolid
            Gr4.MoveRelative 0#, 0#, -J / 2
'#################################################################
'################################################################
' Boolean operations
Dim Bool3D As Boolean3D
    Set Bool3D = New Boolean3D
Dim Res As Long
Dim GrIndex As Long
    
Dim GrRes1 As Graphic
    Gr1.Update
    Gr2.Update
    Set GrRes1 = Bool3D.Subtract(Gr1, Gr2)
    If (Not GrRes1 Is Nothing) Then
        Gr1.Deleted = True
        Gr2.Deleted = True
        Grs.AddGraphic GrRes1
    End If
    
 Dim GrRes2 As Graphic
    GrRes1.Update
    Gr3.Update
    Set GrRes2 = Bool3D.Add(GrRes1, Gr3)
    If (Not GrRes2 Is Nothing) Then
        GrRes1.Deleted = True
        Gr3.Deleted = True
        Grs.AddGraphic GrRes2
    End If
   
 Dim GrRes3 As Graphic
    GrRes2.Update
    Gr4.Update
    Set GrRes3 = Bool3D.Subtract(GrRes2, Gr4)
    If (Not GrRes3 Is Nothing) Then
        GrRes2.Deleted = True
        Gr4.Deleted = True
        Grs.AddGraphic GrRes3
    End If
    
    GrIndex = GrRes3.Index
    Grs.Remove GrIndex
    Gr.Graphics.AddGraphic GrRes3
    GrRes3.Cosmetic = True
    GrRes3.Properties("PenColor") = Gr.Properties("PenColor")
'?????????????????????????????????????????????????????????????
Dim GrLine As Graphic
    Set GrLine = Grs.Add(11)
    GrLine.Vertices.UseWorldCS = True
    With GrLine.Vertices
        .Add 0, 0, 0
        .Add L, 0, 0
    End With
    GrLine.Properties("PenColor") = RGB(255, 0, 0)
    GrIndex = GrLine.Index
    Grs.Remove GrIndex
    Gr.Graphics.AddGraphic GrLine
    GrLine.Cosmetic = True
Dim xx0#, yy0#, zz0#, xx1#, yy1#, zz1#
    GrLine.Vertices.UseWorldCS = False
    With GrLine.Vertices
        xx0 = .Item(0).X
        yy0 = .Item(0).Y
        zz0 = .Item(0).Z
        xx1 = .Item(1).X
        yy1 = .Item(1).Y
        zz1 = .Item(1).Z
    End With
    Gr.Vertices.UseWorldCS = False
    With Gr.Vertices
        X00New = .Item(0).X
        Y00New = .Item(0).Y
        Z00New = .Item(0).Z
    End With
    GrRes3.MoveRelative X00New - xx0, Y00New - yy0, Z00New - zz0
    GrLine.Deleted = True
'?????????????????????????????????????????????????????????????

    Set Gr1 = Nothing
    Set Gr2 = Nothing
    Set Gr3 = Nothing
    Set Gr4 = Nothing
    Set GrRes1 = Nothing
    Set GrRes2 = Nothing
    Set GrRes3 = Nothing
    Set GrTemp = Nothing
    Set Grs = Nothing
    Set Bool3D = Nothing
    
Exit Sub
Failed:

        If Err.Number <> 0 Then
            MsgBox "Regen error: " & Err.Description
        End If
End Sub




' HexFlange Screw

Private Sub HexFlScrew(Gr As Graphic, dd As Double, L As Double, Salp As Double, Calp As Double, lThread As Double, SSolid As Integer)
On Error GoTo Failed
Dim D#, Dw#, Di#, C#, k1#, k#, E#, del#
        D = 1.4 * dd
        Dw = 2.08 * dd
        C = 0.15 * dd
        k1 = 0.43 * dd
        k = 0.97 * dd
        E = (k - k1) * 0.85
        Di = D * Sin(Pi / 3)
        del = 0.1 * dd

Dim Grs As Graphics
    Set Grs = Gr.Application.ActiveDrawing.Graphics
Dim X00#, Y00#, Z00#, X01#, Y01#, Z01#
Dim AddScale#, L1#
        AddScale = 1
        Gr.Vertices.UseWorldCS = True
        With Gr.Vertices
            X00 = .Item(0).X
            Y00 = .Item(0).Y
            Z00 = .Item(0).Z
            X01 = .Item(1).X
            Y01 = .Item(1).Y
            Z01 = .Item(1).Z
            L1 = Sqr((X01 - X00) * (X01 - X00) + (Y01 - Y00) * (Y01 - Y00) + (Z01 - Z00) * (Z01 - Z00))
            If Abs(L + k - L1) > 0.001 Then
                .Item(0).X = X00 - (X01 - X00) * k / AddScale / L1
                .Item(0).Y = Y00 - (Y01 - Y00) * k / AddScale / L1
                .Item(0).Z = Z00 - (Z01 - Z00) * k / AddScale / L1
                .Item(1).X = X00 + (X01 - X00) * L / AddScale / L1
                .Item(1).Y = Y00 + (Y01 - Y00) * L / AddScale / L1
                .Item(1).Z = Z00 + (Z01 - Z00) * L / AddScale / L1
                X01 = X00 + (X01 - X00) * L / L1
                Y01 = Y00 + (Y01 - Y00) * L / L1
                Z01 = Z00 + (Z01 - Z00) * L / L1
            End If
        End With
Dim X00New#, Y00New#, Z00New#, X01New#, Y01New#, Z01New#
        X00New = X00
        Y00New = Y00
        Z00New = Z00
        X01New = X01
        Y01New = Y01
        Z01New = Z01
        X00 = 0
        Y00 = 0
        Z00 = 0
        X01 = L
        Y01 = 0
        Z01 = 0
        
Dim X0(100)
Dim Y0(100)
       X0(1) = -0.5 * k
       Y0(1) = 0#
       
       X0(2) = -0.5 * k
       Y0(2) = dd / 2
       
       X0(3) = L - del
       Y0(3) = dd / 2#
       
       X0(4) = 1 * L
       Y0(4) = dd / 2 - del
       
       X0(5) = 1 * L
       Y0(5) = 0#
       
       X0(6) = X0(1)
       Y0(6) = Y0(1)
'-----------------------------------------------------------
Dim i%
       
Dim Gr1 As Graphic
Dim GrTemp As Graphic
'Add child Graphics revolve Contour
            
            Set GrTemp = Grs.Add(gkGraphic)
            
            With GrTemp.Vertices
                For i = 1 To 6
                    .Add X0(i), Y0(i), 0
                Next i
            End With
            GrTemp.Closed = True
            Set Gr1 = Grs.Add(, "TCW40SPIN")
            Gr1.Properties("Solid") = SSolid
            Gr1.Properties("$ROTATIONANGLE") = 2 * Pi
            Gr1.Properties("$ROTATIONCOPY") = 30
            Set GrTemp = Grs.Remove(GrTemp.Index)
            Gr1.Graphics.AddGraphic GrTemp
            Gr1.Vertices.Add X0(1), Y0(1), 0, False, False, False, False, False
            Gr1.Vertices.Add X0(5), Y0(5), 0, False, False, False, False, False
            
' Thread
    'Base contour
Dim hThread# ' Step of the thread
    hThread = del / Sin(Pi / 3)
Dim nCoil As Long ', K As Long
    nCoil = 10
    nCoil = CLng(lThread / hThread)
    
Dim lThreadNew#
    lThreadNew = nCoil * hThread
Dim k2 As Long
    
    X0(1) = L#
    Y0(1) = dd#
    
    X0(2) = L#
    Y0(2) = dd / 2
    
    k2 = 2
    
    For i = 1 To nCoil
        k2 = k2 + 1
        X0(k2) = X0(k2 - 1) - hThread / 2
        Y0(k2) = Y0(k2 - 1) - del
        k2 = k2 + 1
        X0(k2) = X0(k2 - 1) - hThread / 2
        Y0(k2) = Y0(k2 - 1) + del
    Next i
    k2 = k2 + 1
    X0(k2) = L - lThreadNew
    Y0(k2) = dd
    
    k2 = k2 + 1
    X0(k2) = L
    Y0(k2) = dd

    Set GrTemp = Grs.Add(gkGraphic)
    With GrTemp.Vertices
        For i = 1 To k2
            .Add X0(i), Y0(i), 0
        Next i
    End With
    GrTemp.Closed = True

Dim Gr2 As Graphic
    Set Gr2 = Grs.Add(, "TCW40SPIN")
    Gr2.Properties("Solid") = SSolid
    Gr2.Properties("$ROTATIONANGLE") = 2 * Pi
    Gr2.Properties("$ROTATIONCOPY") = 30
    Set GrTemp = Grs.Remove(GrTemp.Index)
    Gr2.Graphics.AddGraphic GrTemp
    Gr2.Vertices.Add X01, Y01, 0, False, False, False, False, False
    Gr2.Vertices.Add X00, Y00, 0, False, False, False, False, False


' Head of the bolt
    For i = 7 To 13
        X0(i) = D / 2 * Cos(Pi / 3 * (i - 7))
        Y0(i) = D / 2 * Sin(Pi / 3 * (i - 7))
    Next i
Dim Gr3 As Graphic
    Set Gr3 = Grs.Add(gkGraphic)
    With Gr3.Vertices
        For i = 7 To 13
            .Add X0(i), Y0(i), 0, True, True, False, False, False, False
        Next i
    End With
    Gr3.Closed = True
    Gr3.Properties("Thickness") = k
    Gr3.Properties("Solid") = SSolid
Dim xTo#, yTo#, zTo#, xFrom#, yFrom#, zFrom#, xRef#, yRef#, zRef#
    xTo = X00 + (X01 - X00) / L
    yTo = Y00 + (Y01 - Y00) / L
    zTo = 0
    xFrom = X00
    yFrom = Y00
    zFrom = 1#
    xRef = X00
    yRef = Y00
    zRef = 0#
    Gr3.RotateAbsolute xTo, yTo, zTo, xFrom, yFrom, zFrom, xRef, yRef, zRef
'################################################################
'################################################################
Dim T#
    T = D / 2 * Sin(Pi / 3)
       X0(1) = -1.1 * k
       Y0(1) = T * 0.7 - 0.1 * k / Tan(Pi / 6)
       
       X0(2) = -1.1 * k
       Y0(2) = D
       
       X0(3) = -k + 0.4 * k
       Y0(3) = D
       
       X0(4) = -k + 0.4 * k
       Y0(4) = T * 0.7 + 0.5 * k / Tan(Pi / 6)
       
       X0(5) = X0(1)
       Y0(5) = Y0(1)
       
'-----------------------------------------------------------
       
            Set GrTemp = Grs.Add(gkGraphic)
            With GrTemp.Vertices
                For i = 1 To 5
                    .Add X0(i), Y0(i), 0
                Next i
            End With
            GrTemp.Closed = True
Dim Gr4 As Graphic
            Set Gr4 = Grs.Add(, "TCW40SPIN")
            Gr4.Properties("Solid") = SSolid
            Gr4.Properties("$ROTATIONANGLE") = 2 * Pi
            Gr4.Properties("$ROTATIONCOPY") = 30
            Set GrTemp = Grs.Remove(GrTemp.Index)
            Gr4.Graphics.AddGraphic GrTemp
            Gr4.Vertices.Add X00, Y00, 0, False, False, False, False, False
            Gr4.Vertices.Add X01, Y01, 0, False, False, False, False, False
'#################################################################
    
'  Begin Flange *******************************************
       X0(1) = -k1
       Y0(1) = 0#
       
       X0(2) = -k1
       Y0(2) = D / 2
       
       X0(3) = -C
       Y0(3) = Dw / 2
       
       X0(4) = 0.01 * k1#
       Y0(4) = Dw / 2
       
       X0(5) = 0.01 * k1
       Y0(5) = 0#
       
       X0(6) = X0(1)
       Y0(6) = Y0(1)
       
'-----------------------------------------------------------
            Set GrTemp = Grs.Add(gkGraphic)
            With GrTemp.Vertices
                For i = 1 To 6
                    .Add X0(i), Y0(i), 0
                Next i
            End With
            GrTemp.Closed = True
Dim Gr5 As Graphic
            Set Gr5 = Grs.Add(, "TCW40SPIN")
            Gr5.Properties("Solid") = SSolid
            Gr5.Properties("$ROTATIONANGLE") = 2 * Pi
            Gr5.Properties("$ROTATIONCOPY") = 30
            Set GrTemp = Grs.Remove(GrTemp.Index)
            Gr5.Graphics.AddGraphic GrTemp
            Gr5.Vertices.Add X0(1), Y0(1), 0, False, False, False, False, False
            Gr5.Vertices.Add X0(5), Y0(5), 0, False, False, False, False, False
'  End Flange ***********************************************
' Boolean operations
Dim Bool3D As Boolean3D
    Set Bool3D = New Boolean3D
Dim Res As Long
Dim GrIndex As Long
    
Dim GrRes1 As Graphic
    Gr1.Update
    Gr2.Update
    Set GrRes1 = Bool3D.Subtract(Gr1, Gr2)
    If (Not GrRes1 Is Nothing) Then
        Gr1.Deleted = True
        Gr2.Deleted = True
        Grs.AddGraphic GrRes1
    End If
    
 Dim GrRes2 As Graphic
    GrRes1.Update
    Gr3.Update
    Set GrRes2 = Bool3D.Add(GrRes1, Gr3)
    If (Not GrRes2 Is Nothing) Then
        GrRes1.Deleted = True
        Gr3.Deleted = True
        Grs.AddGraphic GrRes2
    End If
   
 Dim GrRes3 As Graphic
    GrRes2.Update
    Gr4.Update
    Set GrRes3 = Bool3D.Subtract(GrRes2, Gr4)
    If (Not GrRes3 Is Nothing) Then
        GrRes2.Deleted = True
        Gr4.Deleted = True
        Grs.AddGraphic GrRes3
    End If
    
 Dim GrRes4 As Graphic
    GrRes3.Update
    Gr5.Update
    Set GrRes4 = Bool3D.Add(GrRes3, Gr5)
    If (Not GrRes4 Is Nothing) Then
        GrRes3.Deleted = True
        Gr5.Deleted = True
        Grs.AddGraphic GrRes4
    End If
    
    GrIndex = GrRes4.Index
    Grs.Remove GrIndex
    Gr.Graphics.AddGraphic GrRes4
    GrRes4.Cosmetic = True
    GrRes4.Properties("PenColor") = Gr.Properties("PenColor")
'?????????????????????????????????????????????????????????????
Dim GrLine As Graphic
    Set GrLine = Grs.Add(11)
    GrLine.Vertices.UseWorldCS = True
    With GrLine.Vertices
        .Add -k, 0, 0
        .Add L, 0, 0
    End With
    GrLine.Properties("PenColor") = RGB(255, 0, 0)
    GrIndex = GrLine.Index
    Grs.Remove GrIndex
    Gr.Graphics.AddGraphic GrLine
    GrLine.Cosmetic = True
Dim xx0#, yy0#, zz0#, xx1#, yy1#, zz1#

    GrLine.Vertices.UseWorldCS = False
    With GrLine.Vertices
        xx0 = .Item(0).X
        yy0 = .Item(0).Y
        zz0 = .Item(0).Z
        xx1 = .Item(1).X
        yy1 = .Item(1).Y
        zz1 = .Item(1).Z
    End With
    Gr.Vertices.UseWorldCS = False
    With Gr.Vertices
        X00New = .Item(0).X
        Y00New = .Item(0).Y
        Z00New = .Item(0).Z
    End With
    GrRes4.MoveRelative X00New - xx0, Y00New - yy0, Z00New - zz0
    GrLine.Deleted = True
'?????????????????????????????????????????????????????????????



    Set Gr1 = Nothing
    Set Gr2 = Nothing
    Set Gr3 = Nothing
    Set Gr4 = Nothing
    Set Gr5 = Nothing
    Set GrRes1 = Nothing
    Set GrRes2 = Nothing
    Set GrRes3 = Nothing
    Set GrRes4 = Nothing
    Set GrTemp = Nothing
    Set Grs = Nothing
    Set Bool3D = Nothing
    
Exit Sub
Failed:

        If Err.Number <> 0 Then
            MsgBox "Regen error: " & Err.Description
        End If
End Sub

